﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KnowledgeAssistant
{
    enum ActionType { Add, Edit }
    public partial class TestEditorForm : Form
    {
        private ActionType action;

        private int three;
        private int four;
        private int five;

        private string message;

        private Theme theme;

        private List<Answer> answersToDelete = new List<Answer>();

        private List<Question> first_questions;
        private List<Question> second_questions;

        private List<List<Answer>> first_answers;
        private List<List<Answer>> second_answers;

        public TestEditorForm()
        {
            InitializeComponent();

            action = ActionType.Add;

            RefreshData();

            this.first_questions = new List<Question>() { };
            this.first_answers = new List<List<Answer>>() { };

            this.second_questions = new List<Question>() { };
            this.second_answers = new List<List<Answer>>() { };

            checkBox1.CheckedChanged -= checkBox1_CheckedChanged;

            this.quest_count_label1.Text = "Количество вопросов - 0";
            this.quest_count_label2.Text = "Количество вопросов - 0";
        }

        public TestEditorForm(Theme theme)
        {
            InitializeComponent();

            action = ActionType.Edit;

            this.theme = theme;

            this.themetextBox.Text = theme.Name;
            List<Subject> subjs = SubjectUtil.GetAllSubjects();
            this.subjectcomboBox1.DataSource = subjs;
            this.subjectcomboBox1.DisplayMember = "Name";
            for (int id = 0; id < subjs.Count; id++)
            {
                if (subjs[id].SubjectID == theme.Subject.SubjectID)
                {
                    subjectcomboBox1.SelectedIndex = id;
                }
            }

            this.countOfFirstQuestions.Value = theme.CountOfFirstQuestions;
            this.countOfSecondQuestionsType1.Value = theme.CountOfSecondQuestionsType1;
            this.countOfSecondQuestionsType2.Value = theme.CountOfSecondQuestionsType2;

            this.durationtime.Value = theme.Duration;

            this.three_nud.Value = theme.Three;
            this.good_nud.Value = theme.Four;
            this.best_nud.Value = theme.Five;

            RefreshQuestions();

            this.quest_count_label1.Text = "Количество вопросов - " + questions1.Items.Count;
            this.quest_count_label2.Text = "Количество вопросов - " + questions2.Items.Count;
        }

        private void RefreshQuestions()
        {
            first_questions = TestUtil.GetThemeQuestionsByType(theme, 1);
            second_questions = TestUtil.GetThemeQuestionsByType(theme, 2);
            second_questions.AddRange(TestUtil.GetThemeQuestionsByType(theme, 3));

            first_answers = new List<List<Answer>>();
            second_answers = new List<List<Answer>>();

            foreach (Question q in first_questions)
            {
                first_answers.Add(TestUtil.GetQuestionAnswersByQuestion(q));
            }

            questions1.SelectedIndexChanged -= questions1_SelectedIndexChanged;
            questions1.DataSource = first_questions;
            questions1.DisplayMember = "Description";
            questions1.SelectedIndex = -1;
            questions1.SelectedIndexChanged += questions1_SelectedIndexChanged;

            second_questions = second_questions.OrderBy(q => q.QuestionID).ToList();

            foreach (Question q in second_questions)
            {
                second_answers.Add(TestUtil.GetQuestionAnswersByQuestion(q));
            }

            questions2.SelectedIndexChanged -= questions2_SelectedIndexChanged;
            questions2.DataSource = second_questions;
            questions2.DisplayMember = "Description";
            questions2.SelectedIndex = -1;
            questions2.SelectedIndexChanged += questions2_SelectedIndexChanged;

            checkBox1.CheckedChanged -= checkBox1_CheckedChanged;

            RefreshData();

            if (questions1.Items.Count != 0)
            {
                questions1.SelectedIndex = 0;
            }
            if (questions2.Items.Count != 0)
            {
                questions2.SelectedIndex = 0;
            }
        }

        private void RefreshData()
        {
            this.levelcombobox1.DataSource = QuestionUtil.GetListLevels();
            this.levelcombobox1.DisplayMember = "Description";

            this.levelcomboBox2.DataSource = QuestionUtil.GetListLevels();
            this.levelcomboBox2.DisplayMember = "Description";

            this.picturecheckBox1.Checked = false;
            this.picturecheckBox2.Checked = false;

            this.selectPicturebutton1.Enabled = false;
            this.selectPicturebutton2.Enabled = false;
        }

        private void addquestion_Click(object sender, EventArgs e)
        {
            Question q = new Question(0, null, "Новый вопрос", 1, null);

            q.Level = QuestionUtil.GetLevel("Обычный");
            q.Theme = this.theme;

            first_questions.Add(q);

            first_answers.Add(new List<Answer>() { });

            first_answers[first_questions.Count - 1].Add(new Answer(0, "Да", q, false));
            first_answers[first_questions.Count - 1].Add(new Answer(0, "Нет", q, false));

            questions1.DataSource = null;
            questions1.DataSource = first_questions;
            questions1.DisplayMember = "Description";

            question1_description.Text = q.Description;

            questions1.SelectedIndex = questions1.Items.Count - 1;

            this.quest_count_label1.Text = "Количество вопросов - " + questions1.Items.Count ;
        }

        private void question1_description_KeyUp(object sender, KeyEventArgs e)
        {
            if (questions1.SelectedIndex != -1)
            {
                int old_value = questions1.SelectedIndex;
                first_questions[questions1.SelectedIndex].Description = question1_description.Text;
                questions1.DataSource = null;
                questions1.DataSource = first_questions;
                questions1.DisplayMember = "Description";

                questions1.SelectedIndex = old_value;
            }
        }

        private void questions1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (questions1.SelectedIndex != -1)
            {
                question1_description.Text = ((Question)questions1.Items[questions1.SelectedIndex]).Description;

                answers_table1.Rows.Clear();

                foreach (Answer a in first_answers[questions1.SelectedIndex])
                {
                    answers_table1.Rows.Add(a.Description, a.True);
                }

                Level l = first_questions[questions1.SelectedIndex].Level;
                foreach (Level lvl in levelcombobox1.Items)
                {
                    if (l.Description == lvl.Description)
                    {
                        levelcombobox1.SelectedItem = lvl;
                    }
                }

                if (((Question)questions1.Items[questions1.SelectedIndex]).Image != null)
                {
                    picturecheckBox1.Checked = true;
                    pictureBox1.Image = ((Question)questions1.Items[questions1.SelectedIndex]).Image;
                    selectPicturebutton1.Enabled = true;
                }
                else
                {
                    picturecheckBox1.Checked = false;
                }

            }
        }

        private void answers_table1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            bool new_value = (bool)answers_table1.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue;
            if (new_value)
            {
                for (int i = 0; i < answers_table1.Rows.Count; i++)
                {
                    if (i != e.RowIndex)
                    {
                        ((DataGridViewCheckBoxCell)answers_table1.Rows[i].Cells[1]).Value = false;
                        first_answers[questions1.SelectedIndex][i].True = false;
                    }
                    else
                    {
                        ((DataGridViewCheckBoxCell)answers_table1.Rows[i].Cells[1]).Value = true;
                        first_answers[questions1.SelectedIndex][i].True = true;
                    }
                }
            }
            else
            {
                first_answers[questions1.SelectedIndex][e.RowIndex].True = false;
            }
        }

        private void addquestion2_Click(object sender, EventArgs e)
        {
            Question q = new Question(0, null, "Новый вопрос", (byte)((checkBox1.Checked) ? 2 : 3), null);

            q.Level = QuestionUtil.GetLevel("Обычный");
            q.Theme = this.theme;

            second_questions.Add(q);

            second_answers.Add(new List<Answer>() { });

            questions2.DataSource = null;
            questions2.DataSource = second_questions;
            questions2.DisplayMember = "Description";

            question_description2.Text = q.Description;

            questions2.SelectedIndex = questions2.Items.Count - 1;

            this.quest_count_label2.Text = "Количество вопросов - " + questions2.Items.Count;
        }

        private void questions2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (questions2.SelectedIndex != -1)
            {
                Question question_t = (Question)questions2.Items[questions2.SelectedIndex];
                question_description2.Text = question_t.Description;
                checkBox1.CheckedChanged -= checkBox1_CheckedChanged;
                checkBox1.Checked = (second_questions[questions2.SelectedIndex].Type == 2) ? true : false;
                checkBox1.CheckedChanged += checkBox1_CheckedChanged;

                answers_table2.Rows.Clear();

                foreach (Answer a in second_answers[questions2.SelectedIndex].Where(a => a.ToDelete == false || a.ToDelete == null))
                {
                    answers_table2.Rows.Add(a.Description, a.True);
                }

                Level l = second_questions[questions2.SelectedIndex].Level;
                foreach (Level lvl in levelcomboBox2.Items)
                {
                    if (l.Description == lvl.Description)
                    {
                        levelcomboBox2.SelectedItem = lvl;
                    }
                }

                if (((Question)questions2.Items[questions2.SelectedIndex]).Image != null)
                {
                    picturecheckBox2.Checked = true;
                    pictureBox2.Image = ((Question)questions2.Items[questions2.SelectedIndex]).Image;
                    selectPicturebutton2.Enabled = true;
                }
                else
                {
                    picturecheckBox2.Checked = false;
                }
            }
        }

        private void question_description2_KeyUp(object sender, KeyEventArgs e)
        {
            if (questions2.SelectedIndex != -1)
            {
                int old_value = questions2.SelectedIndex;
                second_questions[questions2.SelectedIndex].Description = question_description2.Text;
                questions2.DataSource = null;
                questions2.DataSource = second_questions;
                questions2.DisplayMember = "Description";

                questions2.SelectedIndex = old_value;
            }
        }

        private void addanswer2_Click(object sender, EventArgs e)
        {
            if (questions2.SelectedIndex > -1)
            {
                answers_table2.Rows.Add("Новый ответ" + (second_answers[questions2.SelectedIndex].Count + 1).ToString(), false);
                second_answers[questions2.SelectedIndex].Add(new Answer(0, "Новый ответ" 
                + (second_answers[questions2.SelectedIndex].Count+1).ToString()
                    , null, false));
            }
        }

        private void answers_table2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex != 0)
                {
                    bool new_value = (bool)answers_table2.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue;
                    if (checkBox1.Checked)
                    {
                        if (new_value)
                        {
                            for (int i = 0; i < answers_table2.Rows.Count; i++)
                            {
                                if (i != e.RowIndex)
                                {
                                    ((DataGridViewCheckBoxCell)answers_table2.Rows[i].Cells[1]).Value = false;
                                    second_answers[questions2.SelectedIndex][i].True = false;
                                }
                                else
                                {
                                    ((DataGridViewCheckBoxCell)answers_table2.Rows[i].Cells[1]).Value = true;
                                    second_answers[questions2.SelectedIndex][i].True = true;
                                }
                            }
                        }
                        else
                        {
                            second_answers[questions2.SelectedIndex][e.RowIndex].True = false;
                        }
                    }
                    else
                    {
                        ((DataGridViewCheckBoxCell)answers_table2.Rows[e.RowIndex].Cells[1]).Value = new_value;
                        second_answers[questions2.SelectedIndex][e.RowIndex].True = new_value;
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked && questions2.SelectedIndex > -1)
            {
                second_questions[questions2.SelectedIndex].Type = 2;

                for (int i = 0; i < answers_table2.Rows.Count; i++)
                {
                    ((DataGridViewCheckBoxCell)answers_table2.Rows[i].Cells[1]).Value = false;
                    second_answers[questions2.SelectedIndex][i].True = false;
                }
            }
            else if (!checkBox1.Checked && questions2.SelectedIndex > -1)
            {
                second_questions[questions2.SelectedIndex].Type = 3;
            }
        }

        private void removequestion_Click(object sender, EventArgs e)
        {
            if (questions1.SelectedIndex > -1)
            {
                int old_value = questions1.SelectedIndex;

                first_answers.RemoveAt(questions1.SelectedIndex);
                first_questions.RemoveAt(questions1.SelectedIndex);

                questions1.DataSource = null;
                questions1.DataSource = first_questions;
                questions1.DisplayMember = "Description";

                if (questions1.Items.Count > 0)
                {
                    questions1.SelectedIndex = (old_value - 1 > -1) ? old_value - 1 : 0;
                }
                else
                {
                    answers_table1.Rows.Clear();
                    question1_description.Text = "";
                }

                this.quest_count_label1.Text = "Количество вопросов - " + questions1.Items.Count;
            }
        }

        private void removequestion2_Click(object sender, EventArgs e)
        {
            if (questions2.SelectedIndex > -1)
            {
                int old_value = questions2.SelectedIndex;

                second_answers.RemoveAt(questions2.SelectedIndex);
                second_questions.RemoveAt(questions2.SelectedIndex);

                questions2.DataSource = null;
                questions2.DataSource = second_questions;
                questions2.DisplayMember = "Description";

                if (questions2.Items.Count > 0)
                {
                    questions2.SelectedIndex = (old_value - 1 > -1) ? old_value - 1 : 0; ;
                }
                else
                {
                    answers_table2.Rows.Clear();
                    question_description2.Text = "";
                }

                this.quest_count_label2.Text = "Количество вопросов - " + questions2.Items.Count;
            }
        }

        private void answers_table2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int index = 0;
            if ((index = questions2.SelectedIndex) > -1)
            {
                second_answers[index][e.RowIndex].Description = answers_table2.Rows[e.RowIndex].Cells[0].Value.ToString();
            }
        }

        private void TestEditorForm_Load(object sender, EventArgs e)
        {

        }

        private void levelcombobox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (questions1.SelectedIndex != -1 && levelcombobox1.SelectedIndex != -1)
            {
                first_questions[questions1.SelectedIndex].Level = (Level)(levelcombobox1.SelectedItem);
            }
        }

        private void levelcomboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (questions2.SelectedIndex != -1 && levelcomboBox2.SelectedIndex != -1)
            {
                second_questions[questions2.SelectedIndex].Level = (Level)(levelcomboBox2.SelectedItem);
            }
        }

        private void picturecheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (questions1.SelectedIndex != -1)
            {
                if (picturecheckBox1.Checked)
                {
                    selectPicturebutton1.Enabled = true;
                }
                else
                {
                    selectPicturebutton1.Enabled = false;
                    pictureBox1.Image = null;
                }
            }
            else
            {
                selectPicturebutton1.Enabled = false;
                pictureBox1.Image = null;
            }
        }

        private void picturecheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (questions2.SelectedIndex != -1)
            {
                if (picturecheckBox2.Checked)
                {
                    selectPicturebutton2.Enabled = true;
                }
                else
                {
                    selectPicturebutton2.Enabled = false;
                    pictureBox2.Image = null;
                }
            }
            else
            {
                selectPicturebutton2.Enabled = false;
                pictureBox2.Image = null;
            }
        }

        private void selectPicturebutton1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "jpeg|*.jpg";
            ofd.ShowDialog();
            if (ofd.FileName != "" && ofd.FileName != null)
            {
                Bitmap image = new Bitmap(ofd.FileName);
                first_questions[questions1.SelectedIndex].Image = image;
                pictureBox1.Image = image;
            }
        }

        private void selectPisturebutton2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "jpeg|*.jpg";
            ofd.ShowDialog();
            if (ofd.FileName != "" && ofd.FileName != null)
            {
                Bitmap image = new Bitmap(ofd.FileName);
                second_questions[questions2.SelectedIndex].Image = image;
                pictureBox2.Image = image;
            }
        }

        private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (CheckBeforeSave())
            {
                this.theme.CountOfFirstQuestions = (int)countOfFirstQuestions.Value;
                this.theme.CountOfSecondQuestionsType1 = (int)countOfSecondQuestionsType1.Value;
                this.theme.CountOfSecondQuestionsType2 = (int)countOfSecondQuestionsType2.Value;
                this.theme.Duration = (int)durationtime.Value;
                this.theme.Name = this.themetextBox.Text.Trim();
                this.theme.Subject = (Subject)subjectcomboBox1.SelectedItem;

                this.three = (int)three_nud.Value;
                this.four = (int)good_nud.Value;
                this.five = (int)best_nud.Value;

                TestUtil.UpdateTestQuestions(this.theme, first_questions, first_answers, second_questions, second_answers, answersToDelete, three, four, five);
                MessageBox.Show("Сохранение успешно завершено!");
            }
            else { MessageBox.Show(message); }
        }

        private void savetestbutton_Click(object sender, EventArgs e)
        {
            сохранитьToolStripMenuItem_Click(sender, e);
        }

        private bool CheckBeforeSave()
        {
            if (this.themetextBox.Text.Trim() == "")
            {
                this.message = "Задайте имя темы";
                return false;
            }

            if (subjectcomboBox1.SelectedIndex < 0)
            {
                this.message = "Задайте предмет темы";
                return false;
            }

            for (int i = 0; i < questions1.Items.Count; i++)
            {
                if (((Question)questions1.Items[i]).Description == "")
                {
                    this.message = "Задайте формулировку вопроса";
                    return false;
                }
            }

            for (int i = 0; i < questions1.Items.Count; i++)
            {
                if (((Question)questions1.Items[i]).Description == "")
                {
                    this.message = "Задайте формулировку вопроса";
                    return false;
                }
            }

            for (int i = 0; i < first_answers.Count; i++)
            {
                bool isNotChecked = true;
                for (int j = 0; j < first_answers[i].Count; j++)
                {
                    if (first_answers[i][j].True)
                    {
                        isNotChecked = false;
                    }
                    if (first_answers[i][j].Description == "")
                    {
                        this.message = "Задайте формулировку ответа";
                        this.tabPage1.Focus();
                        answers_table1.Focus();
                        answers_table1.Rows[j].Cells[0].Selected = true;
                        return false;
                    }
                }
                questions1.SelectedIndex = i;
                if (isNotChecked)
                {
                    this.message = "Выберите правильный ответ";
                    return false;
                }
            }

            for (int i = 0; i < second_answers.Count; i++)
            {               
                if (second_answers[i].Where(a=>a.ToDelete == false).ToList().Count > 2
                    && second_answers[i].Where(a => a.ToDelete == false).ToList().Count < 7)
                {
                    bool isNotChecked = true;
                    for (int j = 0; j < second_answers[i].Count; j++)
                    {
                        if (second_answers[i][j].True)
                        {
                            isNotChecked = false;
                        }

                        if (second_answers[i][j].Description == "")
                        {
                            this.message = "Задайте формулировку ответа";
                            this.tabPage2.Focus();
                            answers_table2.Focus();
                            answers_table2.Rows[j].Cells[0].Selected = true;
                            return false;
                        }
                    }
                    questions2.SelectedIndex = i;
                    if (isNotChecked)
                    {
                        this.message = "Выберите правильный ответ";
                        return false;
                    }
                }
                else
                {
                    questions2.SelectedIndex = i;
                    this.message = "Количество ответов на вопрос второго блока долно быть от 3 до 6 ответов";
                    return false;
                }
            }

            if (this.countOfFirstQuestions.Value > questions1.Items.Count)
            {
                this.message = "Количество вопросов первого блока для теста должно быть меньше или равно общему количеству введенных вопросов";
                this.countOfFirstQuestions.Focus();
                return false;
            }

            if (this.countOfSecondQuestionsType1.Value + this.countOfSecondQuestionsType2.Value > questions2.Items.Count)
            {
                this.message = "Общее количество вопросов типа 1 и типа 2 второго блока для теста должно быть меньше или равно общему количеству введенных вопросов";
                this.countOfSecondQuestionsType1.Focus();
                return false;
            }

            return true;

        }

        private void exitbutton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void removeanswer2_Click(object sender, EventArgs e)
        {
            if (questions2.SelectedIndex > -1)
            {
                if (answers_table2.SelectedRows[0].Index > -1)
                {
                    Answer answer = second_answers[questions2.SelectedIndex][answers_table2.SelectedRows[0].Index];                    
                    second_answers[questions2.SelectedIndex].RemoveAt(answers_table2.SelectedRows[0].Index);
                    answers_table2.Rows.RemoveAt(answers_table2.SelectedRows[0].Index);
                    if (answer.AnswerID != 0)
                    {
                        answersToDelete.Add(answer);
                    }
                }
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (this.pictureBox1.Image != null)
            {
                FullPictureViewForm fppf = new FullPictureViewForm(this.pictureBox1.Image);
                fppf.ShowDialog();
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (this.pictureBox2.Image != null)
            {
                FullPictureViewForm fppf = new FullPictureViewForm(this.pictureBox2.Image);
                fppf.ShowDialog();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.theme.ThemeID != 0)
            {
                ThemeConcateForm tcf = new ThemeConcateForm(this.theme);
                tcf.ShowDialog();
                RefreshQuestions();
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) 
            {
                if (textBox1.Text != "")
                {
                    questions1.SelectedIndexChanged -= questions1_SelectedIndexChanged;
                    questions1.DataSource = TestUtil.SearchQuestionByDescription(theme.ThemeID, textBox1.Text);
                    questions1.DisplayMember = "Description";
                    questions1.SelectedIndex = -1;
                    questions1.SelectedIndexChanged += questions1_SelectedIndexChanged;

                    checkBox1.CheckedChanged -= checkBox1_CheckedChanged;

                    RefreshData();

                    if (questions1.Items.Count != 0)
                    {
                        questions1.SelectedIndex = 0;
                    }
                    
                }
                else
                {
                    first_questions = TestUtil.GetThemeQuestionsByType(theme, 1);

                    first_answers = new List<List<Answer>>();

                    foreach (Question q in first_questions)
                    {
                        first_answers.Add(TestUtil.GetQuestionAnswersByQuestion(q));
                    }

                    questions1.SelectedIndexChanged -= questions1_SelectedIndexChanged;
                    questions1.DataSource = first_questions;
                    questions1.DisplayMember = "Description";
                    questions1.SelectedIndex = -1;
                    questions1.SelectedIndexChanged += questions1_SelectedIndexChanged;

                    checkBox1.CheckedChanged -= checkBox1_CheckedChanged;

                    RefreshData();

                    if (questions1.Items.Count != 0)
                    {
                        questions1.SelectedIndex = 0;
                    }
                    questions1_SelectedIndexChanged(sender, e);
                }
            }
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (textBox2.Text != "")
                {
                    questions2.SelectedIndexChanged -= questions2_SelectedIndexChanged;
                    questions2.DataSource = TestUtil.SearchQuestionByDescription(theme.ThemeID, textBox2.Text);
                    questions2.DisplayMember = "Description";
                    questions2.SelectedIndex = -1;
                    questions2.SelectedIndexChanged += questions2_SelectedIndexChanged;

                    checkBox1.CheckedChanged -= checkBox1_CheckedChanged;

                    RefreshData();

                    if (questions2.Items.Count != 0)
                    {
                        questions2.SelectedIndex = 0;
                    }
                    questions2_SelectedIndexChanged(sender, e);                }
                else
                {                    
                    questions2.SelectedIndexChanged -= questions2_SelectedIndexChanged;
                    questions2.DataSource = second_questions;
                    questions2.DisplayMember = "Description";
                    questions2.SelectedIndex = -1;
                    questions2.SelectedIndexChanged += questions2_SelectedIndexChanged;

                    checkBox1.CheckedChanged -= checkBox1_CheckedChanged;

                    RefreshData();

                    if (questions2.Items.Count != 0)
                    {
                        questions2.SelectedIndex = 0;
                    }
                    questions2_SelectedIndexChanged(sender, e);
                }
            }
        }
    }
}
